package com.mb.microservice.framework.core.entity;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.mb.microservice.framework.exception.ResultEnum;
import lombok.Data;

/**
 * @PackageName: com.mb.fabric.hongda.finance.entity
 * @ClassName: Result
 * @Description: 返回结果封装
 * @author: ABing
 * @date: 2020/9/14
 */
@Data
@JsonIgnoreProperties(ignoreUnknown = true)
public class Result {
    private Integer status;
    private String msg;
    private Object data;
    
    //json工具类（fastjson、jackson、gson等）会默认调用无参数构造方法
    public Result() {
    }
    
    public Result(Integer status, String msg) {
        this.status = status;
        this.msg = msg;
    }
    
    public Result(Integer status, String msg, Object data) {
        this.status = status;
        this.msg = msg;
        this.data = data;
    }
    
    public static Result success() {
        Result result = new Result();
        result.status = ResultEnum.SUCCESS.getCode();
        result.msg = "success";
        return result;
    }
    
    public static Result success(Object data) {
        Result result = new Result();
        result.status = ResultEnum.SUCCESS.getCode();
        result.msg = "success";
        result.setData(data);
        return result;
    }
    
    public static Result failed(Integer status, String msg) {
        Result result = new Result();
        result.status = status;
        result.msg = msg;
        return result;
    }
    
    public static Result build(Integer status, String msg) {
        Result result = new Result();
        result.status = status;
        result.msg = msg;
        return result;
    }
    
    public static Result build(Integer status, String msg, Object data) {
        Result result = new Result();
        result.status = status;
        result.msg = msg;
        result.data = data;
        return result;
    }
    
    @Override
    public String toString() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status", status);
        jsonObject.put("msg", msg);
        jsonObject.put("data", data);
        return jsonObject.toJSONString();
    }
}
